package training.sample.app.web;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import training.sample.app.domain.Client;
import training.sample.app.service.ClientService;
import training.sample.app.web.view.dto.ClientAjaxDTO;

@Controller
@RequestMapping("/client")
public class ClientAjaxController {

	@Autowired
	private ClientService clientService;

	@RequestMapping(value="/info", method= { RequestMethod.POST })
    @ResponseBody
    public Client clientInfo(
    		@RequestBody final Client client,
			HttpSession session, Model model) {
		System.out.println("test: " + client.getId());
		Client clientData = clientService.getClientByID(client.getId());
		//if (clientData != null) {
		//}
        return clientData;
    }
	
	@RequestMapping(value="/delete/{id}/ajax", method= { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public ClientAjaxDTO clientDelete(
    		@PathVariable("id") Client client,
			HttpSession session, Model model) {
		
		System.out.println("test: " + client.getId());
		clientService.deleteClient(client);
		
        return new ClientAjaxDTO(client);
    }
}